package com.ias.agdis_r.activity.task;

import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
//import android.support.v4.app.Fragment;
import androidx.fragment.app.Fragment;
//import android.support.v4.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentPagerAdapter;
//import android.support.v4.view.ViewPager;
import androidx.viewpager.widget.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ias.agdis_r.R;
import com.ias.agdis_r.activity.BaseKeyFragmentActivity;
import com.ias.agdis_r.activity.MainActivity;
import com.ias.agdis_r.bean.auth.AuthEnum;
import com.ias.agdis_r.bean.user.UserInfoBean;
import com.ias.agdis_r.fragment.task.FTaskHisListFragment;
import com.ias.agdis_r.fragment.task.NFTaskHisListFragment;
import com.ias.agdis_r.manager.JavaBeanManager;
import com.ias.agdis_r.view.control.MutipleTouchViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by guoxr on 2015/9/21.
 */
public class TaskHisAcitivty extends BaseKeyFragmentActivity implements
        View.OnClickListener{
    private MutipleTouchViewPager mViewPager;
    private ActionBar actionBar;
    /**
     * 三个Fragment
     */
    private FTaskHisListFragment fragmentFTask;
    private NFTaskHisListFragment fragmentNFTask;
    /**
     * 顶部的三个TextView
     */
    private TextView txt_ftask_label;
    private TextView txt_nftask_label;
    /**
     * 顶部三个LinearLayout
     */
    private LinearLayout layout_ftask_tab;
    private LinearLayout layout_nftask_tab;
    /**
     * Tab的那个引导线
     */
    private ImageView mTabLine;
    /**
     * 屏幕的宽度
     */
    private int screenWidth;
    /**
     *
     */
    private LinearLayout layout_task_type;

    /**
     * ViewPager的当前选中页
     */
    private int currentIndex = JavaBeanManager.getInstance().getUserinfo()
            .getTaskCurrentIndex();
    private FragmentPagerAdapter mAdapter;
    private List<Fragment> mFragments = new ArrayList<Fragment>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.task_main);
        actionBar = getActionBar();
        taskHisActionbar();
        mViewPager = (MutipleTouchViewPager) findViewById(R.id.id_viewpager);
        layout_task_type = (LinearLayout)findViewById(R.id.layout_task_type);
        initView();
        if (!JavaBeanManager.getInstance().adjust_pri(AuthEnum.AI_T_EVENTTASK.toString())) {
            layout_task_type.setVisibility(View.GONE);
            mViewPager.setNoScroll(true);
            mViewPager.setCurrentItem(0);
        } else {
            layout_task_type.setVisibility(View.VISIBLE);
            mViewPager.setNoScroll(false);
            initTabLine(currentIndex);
        }
        setListener();
    }

    /**
     * 初始化控件，初始化Fragment
     */
    private void initView() {
        layout_ftask_tab = (LinearLayout) findViewById(R.id.id_tab_ftask);
        layout_nftask_tab = (LinearLayout) findViewById(R.id.id_tab_nftask);

        layout_ftask_tab.setOnClickListener(this);
        layout_nftask_tab.setOnClickListener(this);

        txt_ftask_label = (TextView)findViewById(R.id.id_ftask);
        txt_nftask_label = (TextView) findViewById(R.id.id_nftask);

        fragmentFTask = new FTaskHisListFragment();
        fragmentNFTask = new NFTaskHisListFragment();
        mFragments.add(fragmentFTask);
        mFragments.add(fragmentNFTask);


        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return mFragments.size();
            }

            @Override
            public Fragment getItem(int arg0) {
                return mFragments.get(arg0);
            }
        };
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOffscreenPageLimit(1);
    }

    /**
     * 根据屏幕的宽度，初始化引导线的宽度
     *
     * @param page
     */
    private void initTabLine(int page) {
        mTabLine = (ImageView) findViewById(R.id.id_tab_line);
        DisplayMetrics outMetrics = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay()
                .getMetrics(outMetrics);
        screenWidth = outMetrics.widthPixels;
        LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mTabLine
                .getLayoutParams();
        lp.width = screenWidth / 2;
        if (page != -1) {
            lp.leftMargin = (int) (screenWidth * 1.0 / 2) * page;
            mTabLine.setLayoutParams(lp);
        } else {
            mTabLine.setLayoutParams(lp);
        }
    }

    private void setListener(){
        /**
         * 设置监听
         */
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                resetTextView();
                switch (position) {
                    case 0:
                        txt_ftask_label.setTextColor(getResources().getColor(R.color.green));
                        break;
                    case 1:
                        txt_nftask_label.setTextColor(getResources().getColor(R.color.green));
                        break;
                }
                // 重置所有TextView的字体颜色
                currentIndex = position;
                UserInfoBean userInfo = JavaBeanManager.getInstance().getUserinfo();
                if (userInfo != null) {
                    JavaBeanManager.getInstance().getUserinfo().setTaskCurrentIndex(currentIndex);
                }
            }

            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPixels) {
                /**
                 * 利用position和currentIndex判断用户的操作是哪一页往哪一页滑动
                 * 然后改变根据positionOffset动态改变TabLine的leftMargin
                 */
                if (currentIndex == 0 && position == 0)// 0->1
                {
                    LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mTabLine
                            .getLayoutParams();
                    lp.leftMargin = (int) (positionOffset
                            * (screenWidth * 1.0 / 2) + currentIndex
                            * (screenWidth / 2));
                    mTabLine.setLayoutParams(lp);

                } else if (currentIndex == 1 && position == 0) // 1->0
                {
                    LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mTabLine
                            .getLayoutParams();
                    lp.leftMargin = (int) (-(1 - positionOffset)
                            * (screenWidth * 1.0 / 2) + currentIndex
                            * (screenWidth / 2));
                    mTabLine.setLayoutParams(lp);
                } else if (currentIndex == 1 && position == 1) // 1->2
                {
                    LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mTabLine
                            .getLayoutParams();
                    lp.leftMargin = (int) (positionOffset
                            * (screenWidth * 1.0 / 2) + currentIndex
                            * (screenWidth / 2));
                    mTabLine.setLayoutParams(lp);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        if (currentIndex == -1) {
            mViewPager.setCurrentItem(0);
            txt_ftask_label.setTextColor(getResources().getColor(R.color.green));
        }
        else {
            // 当currentIndex为0时启动activity不执行onPageSelected
            if (currentIndex == 0) {
                txt_ftask_label.setTextColor(getResources().getColor(R.color.green));
            }
            mViewPager.setCurrentItem(currentIndex);
        }

    }

    private void taskHisActionbar() {
        // TODO Auto-generated method stub
        actionBar.setCustomView(LayoutInflater.from(TaskHisAcitivty.this).inflate(R.layout.actionbar_back_and_home, null));
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setDisplayShowCustomEnabled(true);
        TextView title = (TextView) findViewById(R.id.txt_title);
        title.setText(R.string.TaskHisAcitivty);
        ImageView back = (ImageView) findViewById(R.id.action_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                finish();
            }
        });
        ImageView home = (ImageView) findViewById(R.id.action_home);
        home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TaskHisAcitivty.this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });
    }

    /**
     * 重置颜色
     */
    protected void resetTextView() {
        txt_ftask_label.setTextColor(getResources().getColor(R.color.black));
        txt_nftask_label.setTextColor(getResources().getColor(R.color.black));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.id_tab_ftask:
                mViewPager.setCurrentItem(0);
                break;
            case R.id.id_tab_nftask:
                mViewPager.setCurrentItem(1);
                break;
        }
    }
}
